package com.ry.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.groovy.GJson;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class LoginTest {

	private static final String HOST_URL = "http://www.rongyitech.com:6060";

	public LoginTest() {

	}
	
	public static void main(String[] args) {
		
		Login("/user_sessions");
	}
	
	public static void Login(String key) {
		
		String url = HOST_URL + key;
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		try {
//			utf8:✓
//			authenticity_token:+RELytAu1P19+G2SFAfHJ6WUqAov/h/SFGkHWJf65uM=
//			[username]:kwashing
//			[password]:123456
//			commit:登录
			
//			List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
//			nvps.add(new BasicNameValuePair("[username]", "kwashing"));  
//			nvps.add(new BasicNameValuePair("[password]", "123456"));  
//			nvps.add(new BasicNameValuePair("commit", "登录"));  
//			httpPost.setEntity(new UrlEncodedFormEntity(nvps));
			
//			JsonObject obj = new JsonObject(); 
//			obj.addProperty("[username]", "kwashing");
//			obj.addProperty("[password]", "123456");
//			StringEntity se = new StringEntity(obj.toString());   
			
			StringEntity se = new StringEntity("{\"[username]\":\"kwashing\",\"[password]\":\"123456\",\"commit\":\"登录\"}");   
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
			httpPost.setEntity(se);
			
			HttpResponse response = client.execute(httpPost);
			HttpEntity entity = response.getEntity();
			
			if (entity != null) {
				InputStream stream = entity.getContent();
				String responseString = convertStreamToString(stream);
				System.out.println("))))"+responseString);
//				DataWrapper wrap = new DataWrapper();
//				wrap = wrap.fromJson(responseString);
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List searchPostalCode(String code) {

		String url = HOST_URL + code;
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();

			if (entity != null) {
				InputStream stream = entity.getContent();
				String responseString = convertStreamToString(stream);

				DataWrapper wrap = new DataWrapper();
				wrap = wrap.fromJson(responseString);
				return wrap.postalCodes;
			}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	private static String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public class DataWrapper {
		public List postalCodes = null;

		public DataWrapper fromJson(String jsonString) {
			return new Gson().fromJson(jsonString, DataWrapper.class);
		}
	}
}
